package com.party.admin.web.controller.contact;

import com.party.admin.web.dto.AjaxResult;
import com.party.admin.web.dto.input.common.CommonInput;
import com.party.common.paging.Page;
import com.party.core.model.contact.Contact;
import com.party.core.model.member.Member;
import com.party.core.service.contact.IContactService;
import com.party.core.service.member.IMemberService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 通讯录
 * 
 * @author Juliana
 *
 */
@Controller
@RequestMapping(value = "/contact")
public class ContactController {

	@Autowired
	private IContactService contactService;
	@Autowired
	private IMemberService memberService;
	

	protected static Logger logger = LoggerFactory.getLogger(ContactController.class);

	/**
	 * 通讯录用户列表
	 *
	 * @param member
	 * @param page
	 * @return
	 */
	@RequestMapping(value = "memberList")
	public ModelAndView memberList(Member member, Page page, CommonInput commonInput, String newStatus) {
		ModelAndView mv = new ModelAndView("contact/memberList");
		try {
			page.setLimit(20);
			Map<String, Object> params = CommonInput.appendParams(commonInput);

			Set<String> statuss = new HashSet<String>();
			if (StringUtils.isNotEmpty(newStatus)) {
				for (String s : newStatus.split(",")) {
					statuss.add(s);
				}
				params.put("userStatus", statuss);
			}

			List<Map<String,Object>>groupMember = contactService.memberPage(member, params, page);

			mv.addObject("groupMember", groupMember);
			mv.addObject("page", page);
			mv.addObject("input", commonInput);
			mv.addObject("status", newStatus);
		} catch (Exception e) {
			logger.error("通讯录用户列表异常", e);
			e.printStackTrace();
		}
		return mv;
	}

	/**
	 * 改变通讯录状态
	 * @param contact
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "changeStatus")
	public AjaxResult changeStatus(Contact contact){
		AjaxResult ajaxResult = new AjaxResult();
		Contact dbContact = contactService.get(contact.getId());
		dbContact.setStatus(contact.getStatus());
		contactService.update(dbContact);
		ajaxResult.setSuccess(true);
		return ajaxResult;
	}
	/**
	 * 通讯录列表
	 *
	 * @param page
	 * @return
	 */
	@RequestMapping(value = "contactList")
	public ModelAndView contactList(Contact contact,Page page) {
		ModelAndView mv = new ModelAndView("contact/contactList");
		try {
			page.setLimit(50);
			
			List<Contact>contacts = contactService.listPage(contact, page);
			Member member = memberService.get(contact.getMemberId());
			mv.addObject("contacts", contacts);
			mv.addObject("page", page);
			mv.addObject("contact",contact);
			mv.addObject("member",member);
		} catch (Exception e) {
			logger.error("通讯录用户列表异常", e);
			e.printStackTrace();
		}
		return mv;
	}
	/**
	 * 删除会员
	 *
	 * @param id 会员id
	 * @return
	 */
/*	@ResponseBody
	@RequestMapping(value = "deleteMember")
	public AjaxResult deleteMember(String id) {
		try {
			if(StringUtils.isNotEmpty(id)){
				memberService.delete(id);
			}
		} catch (Exception e) {
			logger.error("删除会员异常", e);
			return new AjaxResult(false);
		}
		return new AjaxResult(true);
	}*/

	/**
	 * 删除会员下的通讯录人员
	 *
	 * @param id 通讯录人员id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "deleteContact")
	public AjaxResult deleteContact(String id) {
		boolean of = false;
		try {
			if(StringUtils.isNotEmpty(id)){
				contactService.delete(id);
				of = true;
			}
		} catch (Exception e) {
			logger.error("删除通讯录人员异常", e);
			return new AjaxResult(false);
		}
		return new AjaxResult(of);
	}
}
